package cn.wind.xboot.module.web.task;

import cn.wind.xboot.logic.entity.Accessid;
import cn.wind.xboot.logic.service.IAccessidService;
import cn.wind.xboot.module.web.utils.HttpUtils;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *刷新AccessId定时任务
 * @author WangHao
 * @date 2019-03-13
 */
@Component
@Configurable
@EnableScheduling
@Slf4j
public class RefreshAccessIdTask {
    @Autowired
    private IAccessidService accessidService;
    @Scheduled(cron = "0 0/1 * * * ?")
    public void refreshAccessId(){
        String host = "http://47.99.170.25/NNQuery.php?";
        String method = "GET";
        String path2 = "AccessID=000&Act=login";
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        try {
            List<Accessid> list = accessidService.findAll();
            if (list.size() > 0) {
                for (Accessid accessid : list) {
                    //校验AccessIdS是否已经失效
                    String path = "AccessID="+accessid.getAccessId()+"&Act=getUsrInfo";
                    HttpResponse response = HttpUtils.doGet( host, path, method, headers, querys );
                    String str = EntityUtils.toString(response.getEntity());
                    String RetCode = JSONObject.fromObject(str).getString("RetCode");
                    if (!RetCode.equals("0")){
                        //AccessId已失效,重新回去AccessId替换数据库原来的数据
                        Map<String,String> bodys = new HashMap<String,String>( );
                        String method2 = "POST";
                        bodys.put("UsrName","hopdata_test");
                        bodys.put("UsrPwd","0.01qaz");
                        HttpResponse response2 = HttpUtils.doPost(host, path2, method2, headers, querys, bodys);
                        String str2 = EntityUtils.toString(response2.getEntity());
                        String AccessID = JSONObject.fromObject(str2).getString("AccessID");
                        Map<String, String> map = Maps.newHashMap();
                        map.put("AccessID", AccessID);
                        //将AccessID信息保存到数据库
                        accessid.setAccessId(AccessID);
                        accessidService.saveOrUpdate(accessid);
                    }

                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
